package com.bootdo.market.controller;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.market.common.HistoryTodayRedis;
import com.bootdo.market.dao.DuplicateDao;
import com.bootdo.market.domain.ProductAdvpositionDO;
import com.bootdo.market.domain.ProductChannelDO;
import com.bootdo.market.service.ProductAdvpositionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.bootdo.common.utils.TimeUtils.getSecondTimestampTwo;

/**
 * @author jinse
 * @email market@jinse.com
 * @date 2019-04-19 16:09:40
 */

@Controller
@RequestMapping("/market/productAdvposition")
public class ProductAdvpositionController {
    @Autowired
    private ProductAdvpositionService productAdvpositionService;
    @Autowired
    private HistoryTodayRedis historyTodayRedis;
    @Autowired
    private DuplicateDao duplicateDao;

    @GetMapping()
    @RequiresPermissions("market:productAdvposition:productAdvposition")
    String ProductAdvposition() {
        return "market/productAdvposition/productAdvposition";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:productAdvposition:productAdvposition")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ProductAdvpositionDO> productAdvpositionList = productAdvpositionService.list(query);
        int total = productAdvpositionService.count(query);
        PageUtils pageUtils = new PageUtils(productAdvpositionList, total);
        return pageUtils;
    }

    @RequestMapping(value = "/listProductAdvpositionType", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String listProductAdvpositionType(HttpServletRequest request, ProductChannelDO productChannel) {
        List<Map<String, Object>> channelList = productAdvpositionService.listProductAdvpositionType();
        return JSON.toJSONString(channelList);
    }

    @GetMapping("/add")
    @RequiresPermissions("market:productAdvposition:add")
    String add() {
        return "market/productAdvposition/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:productAdvposition:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        ProductAdvpositionDO productAdvposition = productAdvpositionService.get(id);
        model.addAttribute("productAdvposition", productAdvposition);
        return "market/productAdvposition/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:productAdvposition:add")
    public R save(ProductAdvpositionDO productAdvposition) {
        productAdvposition.setAdvpositionid(getSecondTimestampTwo(System.currentTimeMillis()));
        if (productAdvpositionService.save(productAdvposition) > 0) {
            ProductAdvpositionDO aaa = productAdvpositionService.get(productAdvposition.getId());
            if (aaa.getIsdelete() == 1) {
                historyTodayRedis.saveAdvpositionConfigRedis(aaa);
            } else {
                historyTodayRedis.deleteAdvpositionConfigRedis(aaa);
            }
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("market:productAdvposition:edit")
    public R update(ProductAdvpositionDO productAdvposition) {
        productAdvpositionService.update(productAdvposition);
        ProductAdvpositionDO aaa = productAdvpositionService.get(productAdvposition.getId());
        if (aaa.getIsdelete() == 1) {
            historyTodayRedis.saveAdvpositionConfigRedis(aaa);
        } else {
            historyTodayRedis.deleteAdvpositionConfigRedis(aaa);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:productAdvposition:remove")
    public R remove(ProductAdvpositionDO productAdvposition) {
        productAdvpositionService.update(productAdvposition);
        ProductAdvpositionDO aaa = productAdvpositionService.get(productAdvposition.getId());
        if (aaa.getIsdelete() == 1) {
            historyTodayRedis.saveAdvpositionConfigRedis(aaa);
        } else {
            historyTodayRedis.deleteAdvpositionConfigRedis(aaa);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:productAdvposition:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        productAdvpositionService.batchRemove(ids);
        return R.ok();
    }

    @RequestMapping(value = "/productAdvpositionUniqueAdd", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productAdvpositionUniqueAdd(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productAdvpositionUniqueAdd(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping(value = "/productAdvpositionUniqueEdit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productAdvpositionUniqueEdit(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productAdvpositionUniqueEdit(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
